package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.Supplier;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 供应商
 * 
 */
@Component
public class SupplierDao implements BaseDao<Supplier> {
    @Override
    public int insert(Supplier entity) {
        return DBHelper.executeUpdate(
            "insert into supplier (name, phone,address,contactname,contactphone,is_status) values (?,?,?, ?,?,?);",
            entity.getName(), entity.getPhone(), entity.getAddress(), entity.getContactname(), entity.getContactphone(),
            entity.getIs_status());
    }

    @Override
    public int update(Supplier entity) {
        return DBHelper.executeUpdate(
            "UPDATE supplier SET name=?, phone=?, address=?, contactname=?, contactphone=?, is_status=? WHERE id=?;",
            entity.getName(), entity.getPhone(), entity.getAddress(), entity.getContactname(), entity.getContactphone(),
            entity.getIs_status(), entity.getId());
    }

    @Override
    public int delete(Supplier entity) {
        return DBHelper.executeUpdate("update supplier set is_deleted=1 where id=?", entity.getId());
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return DBHelper.executeQuery("select * from supplier where is_deleted=0 and is_status=1");
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        // 传进来一个Object对象,将其强转为所需对象
        Supplier sup = (Supplier)obj[0];
        String sql = "select * from supplier where is_deleted=0 ";
        if (obj[0] != null && !"".equals(sup.getName())) {
            sql += " and name like '%" + sup.getName() + "%'";
        }
        return DBHelper.executeQuery(sql + " limit ?,? ", page.getBegin(), page.getPageSize());
    }

    @Override
    public Map<String, Object> findOne(Supplier entity) {

        return DBHelper.executeQuery("select * from supplier where is_deleted=0 and id=? ", entity.getId()).get(0);
    }

    @Override
    public int count(Object... obj) {
        String sql = "select count(*) counts from supplier where is_deleted=0 ";
        // 传进来一个Object对象,将其强转为所需对象
        Supplier sup = (Supplier)obj[0];
        if (obj[0] != null && !"".equals(sup.getName())) {
            sql += " and name like '%" + sup.getName() + "%'";
        }
        return Integer.valueOf(DBHelper.executeQuery(sql).get(0).get("counts").toString());
    }

    /**
     * 根据供应商 查出所有的金钱
     * 
     * @param id
     * @return
     */
    public List<Map<String, Object>> findSupplier(String id, String begin, String end) {
        String sql =
            "select s.name,po.price from purchase_order po LEFT JOIN supplier s on po.supplier_id = s.id where po.supplier_id=? and po.is_status!=9 and is_pay=0";
        if (begin != null & end != null) {
            sql += " and gmt_modified BETWEEN '" + begin + "' AND '" + end + "' ";
        }

        return DBHelper.executeQuery(sql, id);
    }

    public List<Map<String, Object>> find(String id, String begin, String end) {
        String sql =
            "select po.price from purchase_order po LEFT JOIN supplier s on po.supplier_id = s.id where po.supplier_id=? and po.is_status!=9 and is_pay=1";
        if (begin != null & end != null) {
            sql += " and gmt_modified BETWEEN '" + begin + "' AND '" + end + "' ";
        }

        return DBHelper.executeQuery(sql, id);
    }

    public int update(String id, String begin, String end) {
        String sql = "update purchase_order set is_pay=1 where supplier_id=?";
        if (begin != null & end != null) {
            sql += " and gmt_modified BETWEEN '" + begin + "' AND '" + end + "' ";
        }
        return DBHelper.executeUpdate(sql, id);
    }
}
